package com.longdaji.decoration.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.longdaji.decoration.Constants;
import com.longdaji.decoration.R;
import com.longdaji.decoration.bean.Collection;
import com.longdaji.decoration.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Kanade on 2018/2/27 0027.
 */

public class CollectionAdapter extends RecyclerView.Adapter<CollectionAdapter.ViewHolder> {

    private List<Collection.CollectionGoodsListBean> mCollectionList = new ArrayList<>();

    private Context mContext;

    private int clickMode = Constants.CLICK_MODE_NORMAL;

    private boolean isShowDots = false;
    private boolean isAllChecked = false;

    public CollectionAdapter(List<Collection.CollectionGoodsListBean> collectionList) {
        this.mCollectionList = collectionList;

    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_img)
        ImageView ivImg;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.iv_dot)
        ImageView ivDot;

        private boolean isSelected = false;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (clickMode == Constants.CLICK_MODE_MANAGE) {
                        mViewListener.onViewClickNormal(getAdapterPosition());
                        if (!isSelected) {
                            isSelected = true;
                            Glide.with(mContext).load(R.mipmap.check_state).into(ivDot);
                        } else {
                            isSelected = false;
                            Glide.with(mContext).load(R.mipmap.uncheck_state).into(ivDot);
                        }
                    } else if (clickMode == Constants.CLICK_MODE_NORMAL) {
                        mViewListener.onViewClickManage(mCollectionList.get(getAdapterPosition()));
                    }
                }
            });
        }

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mContext == null) {
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_collection, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Collection.CollectionGoodsListBean collection = mCollectionList.get(position);
        LogUtil.d("mytest","skuImg: "+collection.getGoodsSkuImg());
        LogUtil.d("mytest","collection: "+collection);
        Glide.with(mContext).load(collection.getGoodsSkuImg()).into(holder.ivImg);
        holder.tvName.setText(collection.getGoodsName());
        holder.tvPrice.setText(collection.getGoodsPrice());

        if (isShowDots && !isAllChecked) {
            //重置holder点击事件的isSelected属性
            holder.isSelected = false;
            //设置dot显示和状态
            holder.ivDot.setVisibility(View.VISIBLE);
            //在这里加载，是因为在布局文件中设置test_unchecked的话，在对于选中的item，点击管理，再点一次，有的还是显示checked状态。
            Glide.with(mContext).load(R.mipmap.uncheck_state).into(holder.ivDot);
        } else if (isShowDots && isAllChecked) {
            holder.isSelected = true;
            holder.ivDot.setVisibility(View.VISIBLE);
            Glide.with(mContext).load(R.mipmap.check_state).into(holder.ivDot);
        } else {
            holder.ivDot.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return mCollectionList.size();
    }

    private ViewListener mViewListener;

    public interface ViewListener {
        void onViewClickNormal(int position);

        void onViewClickManage(Collection.CollectionGoodsListBean item);
    }

    public void setOnViewClickListener(ViewListener listener) {
        this.mViewListener = listener;
    }

    public void setClickMode(int mode) {
        this.clickMode = mode;
    }

    public int getClickMode() {
        return clickMode;
    }

    public void showAllIvDot(boolean isShow, boolean isAllChecked) {
        this.isShowDots = isShow;
        this.isAllChecked = isAllChecked;
        notifyDataSetChanged();
    }

    public void setData(List<Collection.CollectionGoodsListBean> list) {
        mCollectionList.clear();
        mCollectionList.addAll(list);
        notifyDataSetChanged();
    }


}
